package com.adinall.dsp.core.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.adinall.dsp.core.globle.Constants;

/**
 * 数字工具类
 * 
 * @author 高万超
 * @version V1.0.0 $ 2016年10月9日 下午11:29:56
 */
public final class NumberUtils {

    public final static String F_INT = ",###,##0";
    public final static String F_DOUBLE = ",###,##0.00";

    public static int parseInt(Object obj, int defVal) {

        try {
            return Integer.parseInt(StringUtils.toString(obj));
        } catch (Exception e) {
            return defVal;
        }
    }

    public static int parseInt(Object obj) {
        return parseInt(obj, 0);
    }

    public static long parseLong(Object obj, long defVal) {

        try {
            return Long.parseLong(StringUtils.toString(obj));
        } catch (Exception e) {
            return defVal;
        }
    }

    public static long parseLong(Object obj) {
        return parseLong(obj);
    }

    public static double parseDouble(Object obj, double defVal) {

        try {
            return Double.parseDouble(StringUtils.toString(obj));
        } catch (Exception e) {
            return defVal;
        }
    }

    public static double parseDouble(Object obj) {
        return parseDouble(obj, 0D);
    }

    public static boolean parseBoolean(Object obj, boolean defVal) {

        try {
            return Boolean.parseBoolean(StringUtils.toString(obj));
        } catch (Exception e) {
            return defVal;
        }
    }

    public static boolean parseBoolean(Object obj) {
        return parseBoolean(obj, false);
    }

    public static BigDecimal parseBigDecimal(Object obj, BigDecimal defVal) {

        try {
            return new BigDecimal(StringUtils.toString(obj));
        } catch (Exception e) {
            return defVal;
        }
    }

    public static BigDecimal parseBigDecimal(Object obj) {
        return parseBigDecimal(obj, BigDecimal.ZERO);
    }

    public static boolean isNum(Object obj) {

        try {
            new BigDecimal(StringUtils.toString(obj));

            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @see NumberUtils#isNum(Object)
     */
    public static boolean isNum(String str) {
        String regEx = "^[0-9]*$";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    public static double max(double... args) {

        if (args == null) {
            return 0;
        }

        double value = Double.MIN_VALUE;

        for (double d : args) {
            value = Math.max(d, value);
        }

        return value;
    }

    public static String format(Object obj, String pattern) {

        if (CheckUtils.isNull(obj)) {
            return Constants.BLANK;
        }

        try {
            return new DecimalFormat(pattern).format(obj);
        } catch (Exception e) {
            e.printStackTrace();
            return Constants.BLANK;
        }
    }
}
